package com.itheima.service.db.impl;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.domain.db.User;
import com.itheima.mapper.UserMapper;
import com.itheima.service.db.UserService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

@DubboService //暴露服务,将对象放入spring容器中
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public Long save(User user) {
        //给密码加密(使用hutool),设置俩时间
        user.setPassword(SecureUtil.md5(user.getPassword()));
        user.setCreated(new Date());
        user.setUpdated(new Date());

        //调用mapper完成保存
        userMapper.insert(user);

        //返回用户id,mp自动主键返回
        return user.getId();
    }

    @Override
    public User findByMobile(String mobile) {
        //创建条件对象
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        qw.eq(User::getMobile,mobile);

        //调用mapper中方法完成查询
        return userMapper.selectOne(qw);
    }
}
